package com.lovely3x.jobservice.executors.extensions.downloader2;

import android.os.Parcel;

import com.litesuits.orm.db.annotation.Column;
import com.litesuits.orm.db.annotation.MapCollection;
import com.litesuits.orm.db.annotation.Mapping;
import com.litesuits.orm.db.annotation.PrimaryKey;
import com.litesuits.orm.db.annotation.Table;
import com.litesuits.orm.db.enums.AssignType;
import com.litesuits.orm.db.enums.Relation;
import com.lovely3x.jobservice.executors.extensions.downloader2.executor.DownloadTaskExecutor;
import com.lovely3x.jobservice.executors.extensions.downloader2.executor.Part;
import com.lovely3x.jobservice.task.Task;

import java.util.ArrayList;
import java.util.List;

/**
 * 下载任务
 * Created by lovely3x on 16-2-4.
 */
@Table(DownloadTaskExecutor.DATA_TABLE)
public class DownloadTask implements Task {

    public static final Creator<DownloadTask> CREATOR = new Creator<DownloadTask>() {
        public DownloadTask createFromParcel(Parcel source) {
            return new DownloadTask(source);
        }

        public DownloadTask[] newArray(int size) {
            return new DownloadTask[size];
        }
    };
    /**
     * 下载地址
     */
    @Column("_url")
    public String downloadUrl;
    /**
     * 保存的位置
     */
    @Column("_path")
    public String storePath;
    /**
     * 请求的方法
     */
    @Column("_method")
    public String requestMethod;
    /**
     * 下载的线程数量
     */
    @Column("_thread_num")
    public int downloadThread;
    /**
     * 下载的块列表
     */
    @Mapping(Relation.OneToMany)
    @MapCollection(ArrayList.class)
    public List<Part> downloadParts;

    /**
     * 任务的标志
     * 用来标志任务，防止任务混乱
     */
    @PrimaryKey(AssignType.BY_MYSELF)
    @Column("_what")
    public long what;
    /**
     * 任务类型
     */
    @Column("_task_type")
    public String taskType;
    /**
     * 延迟执行
     * 默认不延时
     */
    @Column("_delay")
    public long delay;
    /**
     * 任务的提交时间，就是说是什么时候提交任务到任务执行服务的
     */
    @Column("_commit_time")
    public long commitTime;

    public DownloadTask(String downloadUrl, String storePath, String requestMethod, int downloadThread, long what, String taskType) {
        this.downloadUrl = downloadUrl;
        this.storePath = storePath;
        this.requestMethod = requestMethod;
        this.downloadThread = downloadThread;
        this.what = what;
        this.taskType = taskType;
    }

    public DownloadTask() {
    }

    protected DownloadTask(Parcel in) {
        this.downloadUrl = in.readString();
        this.storePath = in.readString();
        this.requestMethod = in.readString();
        this.downloadThread = in.readInt();
        this.downloadParts = new ArrayList<Part>();
        in.readList(this.downloadParts, Part.class.getClassLoader());
        this.what = in.readLong();
        this.taskType = in.readString();
        this.delay = in.readLong();
        this.commitTime = in.readLong();
    }

    public String getDownloadUrl() {
        return downloadUrl;
    }

    public void setDownloadUrl(String downloadUrl) {
        this.downloadUrl = downloadUrl;
    }

    public String getStorePath() {
        return storePath;
    }

    public void setStorePath(String storePath) {
        this.storePath = storePath;
    }

    public String getRequestMethod() {
        return requestMethod;
    }

    public void setRequestMethod(String requestMethod) {
        this.requestMethod = requestMethod;
    }

    public List<Part> getDownloadParts() {
        return downloadParts;
    }

    public void setDownloadParts(List<Part> downloadParts) {
        this.downloadParts = downloadParts;
    }

    @Override
    public long getWhat() {
        return what;
    }

    @Override
    public void setWhat(long what) {
        this.what = what;
    }

    @Override
    public String getTaskType() {
        return taskType;
    }

    @Override
    public void setTaskType(String taskType) {
        this.taskType = taskType;
    }

    @Override
    public long getDelay() {
        return delay;
    }

    @Override
    public void setDelay(long delay) {
        this.delay = delay;
    }

    @Override
    public long getCommitTime() {
        return commitTime;
    }

    @Override
    public void setCommitTime(long commitTime) {
        this.commitTime = commitTime;
    }

    public void setDownloadThread(int downloadThread) {
        this.downloadThread = downloadThread;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(this.downloadUrl);
        dest.writeString(this.storePath);
        dest.writeString(this.requestMethod);
        dest.writeInt(this.downloadThread);
        dest.writeList(this.downloadParts);
        dest.writeLong(this.what);
        dest.writeString(this.taskType);
        dest.writeLong(this.delay);
        dest.writeLong(this.commitTime);
    }


    @Override
    public boolean equals(Object object) {
        if (this == object) return true;
        if (object == null || getClass() != object.getClass()) return false;

        DownloadTask that = (DownloadTask) object;

        return what == that.what;

    }

    @Override
    public int hashCode() {
        return (int) (what ^ (what >>> 32));
    }

    @Override
    public String toString() {
        return "DownloadTask{" +
                "downloadUrl='" + downloadUrl + '\'' +
                ", storePath='" + storePath + '\'' +
                ", requestMethod='" + requestMethod + '\'' +
                ", downloadThread=" + downloadThread +
                ", downloadParts=" + downloadParts +
                ", what=" + what +
                ", taskType='" + taskType + '\'' +
                ", delay=" + delay +
                ", commitTime=" + commitTime +
                '}';
    }
}
